{
  LW Server -- LightWave Plug-in Server

  This unit contains the basic declarations needed to define the
  simplest LightWave plug-in server.

  Based on lwserver.h which is:
  Copyright 1999, NewTek, Inc.
}

{
  Copyright 2012, Jameel Halabi

  This unit is part of the BlaiseWave wrapper,
  which is licensed under the BSD 2-Clause license (see licenses/license.txt).
}


unit LW_Server;

{$mode objfpc}{$H+}

interface

uses
  Classes ;

const
  GFUSE_TRANSIENT = 0;
  GFUSE_ACQUIRE   = 1;
  GFUSE_RELEASE   = 2;

  AFUNC_OK            = 0;
  AFUNC_BADVERSION    = 1;
  AFUNC_BADGLOBAL     = 2;
  AFUNC_BADLOCAL      = 3;
  AFUNC_BADAPP        = 4;
  AFUNC_BADAPP_SILENT = 5;

  {
    This is a selection of some of the more useful language ID's,
    although this list is far from complete.

    Remaining codes can be found by searching the Microsoft Windows documentation
    for OLE LangID codes.

    Japanese strings should be encoded as JIS on Windows and EUC on Unix.
  }
  LANGID_GERMAN             = $0407;
  LANGID_USENGLISH          = $0409;
  LANGID_UKENGLISH          = $0809;
  LANGID_SPANISH            = $040a;
  LANGID_FRENCH             = $040c;
  LANGID_ITALIAN            = $0410;
  LANGID_JAPANESE           = $0411;
  LANGID_KOREAN             = $0412;
  LANGID_RUSSIAN            = $0419;
  LANGID_SWEDISH            = $041D;
  LANGID_CHINESE_SIMPLIFIED = $0804;

  {
    Each server can contain a list of tagged strings which declares some of the
    static information about that server.

    Tags are a bitwise OR of a SrvTag code which selects the type of information,
    and a language ID word which selects the language for which the string is
    valid.
  }
  SRVTAG_USERNAME    = $00000;
  SRVTAG_BUTTONNAME  = $10000;
  SRVTAG_CMDGROUP    = $20000;
  SRVTAG_MENU        = $30000;
  SRVTAG_DESCRIPTION = $40000;
  SRVTAG_ENABLE      = $50000;
  SRVTAG_SELECTCMD   = $60000;
  SRVTAG_NODEGROUP   = $70000;
  SRVTAG_NODECONTEXT = $80000;
  SRVTAG_TOGGLE      = $90000;

  LWSERVERINFO_GLOBAL = 'LW Server Info';

type

  {
    Global Function.

    The global function is a callback provided by the host taking the following
    form:
      - A pointer for the given service name is returned for the given useage.
      - The use codes are for services that will be used once or that will be used
        many times.
  }
  TGlobalFunc = function
                (const ServiceName : PChar  ;
                       UseMode     : Integer) : Pointer;

  {
    Activation Function.

    The server entry poin\ts are all functions of this kind,
    taking a:
      - Version number.
      - Global function pointer.
      - Local (class-specific) data and module data.

    The return value indicates if the service could run and the reason for failure
    if it couldn't.
  }
  TActivateFunc = function
                  (Version    : Integer    ;
                   Global     : TGlobalFunc;
                   Local      : Pointer    ;
                   ServerData : Pointer    ) : Integer;

  {
    Server Definition Record.

    Each server record describes a single activation entry point in the plug-in
    module.

    Each has a:
      - Class.
      - Name.
      - Activation function.
      - Array of tag info records terminated with a zero tag code.
  }
  PServerTagInfo = ^TServerTagInfo;
  TServerTagInfo = record
    String_ : PChar ;
    Tag     : UInt32;
  end;

  PServerRecord = ^TServerRecord;
  TServerRecord = record
    ClassName : PChar         ;
    Name      : PChar         ;
    Activate  : TActivateFunc ;
    TagInfo   : PServerTagInfo;
  end;

  PLWServerInfo = ^TLWServerInfo;
  TLWServerInfo = record
    Path: function
          (const ServerClass : PChar;
           const ServerName  : PChar) : PChar;
    List: function
          (const ServerClass : PChar) : PPChar;
  end;

  TServerUserName = TServerTagInfo;

var
  HandlersList : TFPList;

procedure AddHandler
          (const Name      : PChar         ;
           const ClassName : PChar         ;
           const Activate  : TActivateFunc ;
           const TagInfo   : PServerTagInfo) ;

implementation

procedure AddHandler
          (const Name      : PChar         ;
           const ClassName : PChar         ;
           const Activate  : TActivateFunc ;
           const TagInfo   : PServerTagInfo) ;
var
  NewHandler : PServerRecord;
begin
  New(NewHandler);

  NewHandler^.ClassName := ClassName;
  NewHandler^.Name      := Name;
  NewHandler^.Activate  := TActivateFunc(Activate);
  NewHandler^.TagInfo   := TagInfo;

  if not Assigned(HandlersList) then
    HandlersList := TFPList.Create;

  HandlersList.Add(NewHandler);
end;

end.

